import QtQuick 1.0
import "Dialogs/"
import "ListViews/"
import "Models/"
import "Util/Util.js" as Util


Item {
    id: _root
    anchors.fill: parent
    property real density: 1.0

    property real referenceWidth: parent.width  // ダイアログの横幅を決める参考値

    function open(name){
        _root.state = name;

    }

    function close(){
        _root.state = "";
        _rootContainer.setFocusTextEdit(false);
    }

    /////////////////////////////////////////////
    // 現在表示されてるダイアログのオブジェクトを取得する
    function getActiveDialogObject(){
        var dlg = null;

        switch(state){
        case "Auth":
            dlg = _dialogAuth;
            break;
        case "Setting":
            dlg = _dialogSetting;
            break;
        case "RetweetCaution":
            dlg = _dialogRetweetCaution;
            break;
        case "Message":
            dlg = _dialogMessage;
            break;
        case "FollowCaution":
            dlg = _dialogFollowCaution;
            break;
        case "ExitCaution":
            dlg = _dialogExitCaution;
            break;
        case "AboutCaution":
            dlg = _dialogAbout;
            break;
        case "Help":
            dlg = _dialogHelp;
            break;
        case "Account":
            dlg = _dialogAccount;
            break;
        default:
            break;
        }
        return dlg;
    }

    MouseArea {
        id: _blankArea
        acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MiddleButton
        x: 0
        y: 0
        width: 0
        height: 0
        onClicked: {
        }
    }

    // キーイベント
    function onKeyPressed(event){
        switch(event.key){
        case Qt.Key_Escape:
            close(true);
            event.accepted = true;
            break;
        default:
            break;
        }
    }

    //状態が変わった時の処理
    onStateChanged: {
        var dlg = getActiveDialogObject();
        if(dlg === null){
            // 消えた
            _rootContainer.setFocusTextEdit(false);
        }else{
            //dlg.setForceActiveFocus();
            dlg.viewEvent();
        }
    }

    /////////////////////////////////////
    //アカウント管理ダイアログ
    DialogAccount{
        id: _dialogAccount
        x: (_root.width - width) / 2
        y: (_root.height - height) / 2
        opacity: 0
        density: _root.density

        onAddPressed: {
            _root.state = "Auth";
        }
        onSavePressed: {
            _rootContainer.loadContent();
            close();
        }
        onClosePressed: {
            close();
        }
    }

    /////////////////////////////////////
    //Auth認証のダイアログ
    DialogAuthorization {
        id: _dialogAuth
//        width: 200
//        height: 260
        x: (_root.width - width) / 2
        y: (_root.height - height) / 2
        opacity: 0
        density: _root.density

        onAddPressed: {
            _dialogAccount.addingInfo.oauthToken = twitter.oauthToken;
            _dialogAccount.addingInfo.oauthTokenSecret = twitter.oauthTokenSecret;
            _dialogAccount.addingInfo.screenName = twitter.screenName;
            _dialogAccount.addingInfo.userId = twitter.userId;

            _root.state = "Account";
        }

        onClosePressed: {
            _dialogAccount.addingInfo.clear();
            _root.state = "Account";
        }
    }

    /////////////////////////////////////
    //設定のダイアログ
    DialogSetting {
        id: _dialogSetting
        x: (_root.width - width) / 2
        y: (_root.height - height) / 2
        opacity: 0
        density: _root.density

        //OKボタンイベント
        onClickOkButton: {
            close();
        }
        //キャンセルボタンイベント
        onClickCancelButton: {
            close();
        }
    }

    //////////////////////////////////
    // メッセージダイアログを開く
    // on_func_elmにコールバックしたいエレメントのオブジェクトを指定する
    // で、signal responseDialogMessage(bool ok, variant param)
    // を定義しておくこと
    function openMessage(title, message, on_func_elm, param){
        _dialogMessage.dialogTitle = title;
        _dialogMessage.dialogMessage = message;
        _dialogMessage.onFuncParam = param;
        _dialogMessage.onFuncElm = on_func_elm;

        state = "Message";
    }

    //////////////////////////////////
    //確認ダイアログ
    DialogMessage {
        id: _dialogMessage
        opacity: 0
        x: (_root.width - width) / 2
        y: (_root.height - height) / 2
        density: _root.density

        dialogTitle: "title"
        dialogMessage: "message"

        property variant onFuncElm: null
        property variant onFuncParam: null

        //OKボタンイベント
        onClickOkButton: {
            onFuncElm.responseDialogMessage(true, onFuncParam);
            close();
        }
        //キャンセルボタンイベント
        onClickCancelButton: {
            onFuncElm.responseDialogMessage(false, onFuncParam);
            close();
        }
    }

    ///////////////////////////////////
    //終了確認
    DialogMessage {
        id: _dialogExitCaution
        opacity: 0
        x: (_root.width - width) / 2
        y: (_root.height - height) / 2
        density: _root.density

        dialogTitle: "Attention"
        dialogMessage: "Exit ?"

        //OKボタンイベント
        onClickOkButton: {
            Qt.quit();
        }
        //キャンセルボタンイベント
        onClickCancelButton: {
            close();
        }
    }

    /////////////////////////////////////
    //バージョン情報
    DialogMessage {
        id: _dialogAbout
        opacity: 0
        x: (_root.width - width) / 2
        y: (_root.height - height) / 2
        density: _root.density

        dialogTitle: "About"
        dialogMessage: "Rewit for Qt  Version " + _Setting.versionNumber
                       + "\nCopyright (C) 2011 IoriAYANE"

        visibleCancel: false

        //OKボタンイベント
        onClickOkButton: {
            close();
        }
        //キャンセルボタンイベント
        onClickCancelButton: {
            close();
        }
    }

    /////////////////////////////////////
    //ヘルプ
    DialogMessage {
        id: _dialogHelp
        opacity: 0
        x: (_root.width - width) / 2
        y: (_root.height - height) / 2
        density: _root.density

        dialogTitle: "Help"
        dialogMessage: "Key bind\n"
                       + " - Ctrl+Enter : tweet\n"
                       + " - Enter : select or open context menu\n"
                       + "\n"
                       + " - Tab : move focus\n"
                       + " - Esc : close tweet area\n"
                       + " - F5 : update\n"
                       + "\n"
                       + " - Home : move to top item\n"
                       + " - End : move to bottom item\n"
                       + " - BackSpace : back to Home\n"
                       + " - Cursor Up/Down : move cursor\n"
                       + " - Cursor Left : back to Home\n"
        visibleCancel: false

        //OKボタンイベント
        onClickOkButton: {
            close();
        }
        //キャンセルボタンイベント
        onClickCancelButton: {
            close();
        }
    }

    states: [
        State {
            name: "Account"
            PropertyChanges{
                target: _dialogAccount
                opacity: 1.0
//                y: _root.height * 0.2
            }
            PropertyChanges {
                target: _blankArea
                width: _root.width
                height: _root.height
            }
        },State {
            name: "Auth"
            PropertyChanges{
                target: _dialogAuth
                opacity: 1.0
//                y: _root.height * 0.2
            }
            PropertyChanges {
                target: _blankArea
                width: _root.width
                height: _root.height
            }
        },
        State {
            name: "Setting"
            PropertyChanges{
                target: _dialogSetting
                opacity: 1.0
//                y: _root.height * 0.2
            }
            PropertyChanges {
                target: _blankArea
                width: _root.width
                height: _root.height
            }
        },
        State {
            name: "Message"
            PropertyChanges{
                target: _dialogMessage
                opacity: 1.0
            }
            PropertyChanges {
                target: _blankArea
                width: _root.width
                height: _root.height
            }
        },
        State {
            name: "ExitCaution"
            PropertyChanges{
                target: _dialogExitCaution
                opacity: 1.0
            }
            PropertyChanges {
                target: _blankArea
                width: _root.width
                height: _root.height
            }
        },
        State {
            name: "AboutCaution"
            PropertyChanges{
                target: _dialogAbout
                opacity: 1.0
            }
            PropertyChanges {
                target: _blankArea
                width: _root.width
                height: _root.height
            }
        },
        State {
            name: "Help"
            PropertyChanges{
                target: _dialogHelp
                opacity: 1.0
            }
            PropertyChanges {
                target: _blankArea
                width: _root.width
                height: _root.height
            }
        }
    ]
    transitions: Transition {
        NumberAnimation {
            easing.type: Easing.OutCubic
            targets: [
                    _dialogAccount
                    , _dialogAuth, _dialogSetting
                    , _dialogMessage
                    , _dialogExitCaution
                    , _dialogAbout, _dialogHelp]
            properties: "opacity"
            duration: 500
        }
    }

}
